package com.itheima.service.mongo.impl;

import com.itheima.domain.db.Questionnaire;
import com.itheima.domain.mongo.QuestionnaireLock;
import com.itheima.domain.mongo.SoulUserReport;
import com.itheima.service.mongo.QuestionnaireMongoService;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class QuestionnaireMongoServiceImpl implements QuestionnaireMongoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void insertUserSatus(Long userId, Long questionnaireId) {

        //这个实体类是用户与三张问卷关联信息(主要功能是进行锁定三个问卷)
        QuestionnaireLock questionnaireLock = new QuestionnaireLock();

        questionnaireLock.setUserId(userId);
        questionnaireLock.setQuestionnaireId(questionnaireId);

        mongoTemplate.save(questionnaireLock);

    }

    @Override
    public void unLock(Long userId, Integer level) {
        //哪个用户的哪个等级问卷
        Query query = new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(level));
        Update update = new Update();
        //解锁
        update.set("isLock",0);
        mongoTemplate.updateFirst(query,update,QuestionnaireLock.class);
    }

    @Override
    public QuestionnaireLock findQuestionnaireLock(Long userId, Questionnaire questionnaire) {
        Long questionnaireId = questionnaire.getId();
        Query query = new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(questionnaireId));
        QuestionnaireLock questionnaireLock = mongoTemplate.findOne(query, QuestionnaireLock.class);
        return questionnaireLock;
    }

    @Override
    public SoulUserReport saveReport(SoulUserReport soulUserReport) {
        SoulUserReport userReport = mongoTemplate.save(soulUserReport);
        return userReport;
    }

    @Override
    public SoulUserReport findReportById(String reportId) {
        Query query = new Query(Criteria.where("id").is(reportId));
        SoulUserReport soulUserReport = mongoTemplate.findOne(query, SoulUserReport.class);
        return soulUserReport;
    }

    @Override
    public void updateLock(Long userId, Long id) {
        Query query = new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(id));
        Update update = new Update();
        //解锁
        update.set("isLock",0);
        mongoTemplate.updateFirst(query,update,QuestionnaireLock.class);
    }

    @Override
    public SoulUserReport findReport(Long userId, Integer questionnaireId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("questionnaireId").is(questionnaireId));
        SoulUserReport soulUserReport = mongoTemplate.findOne(query, SoulUserReport.class);
        return soulUserReport;
    }

    @Override
    public List<SoulUserReport> findSimilarUser(Integer score, Long questionnaireId,Long userId) {
        Query query = new Query(Criteria.where("questionnaireId").is(questionnaireId).and("score").is(score).and("userId").ne(userId)).skip(0).limit(10);
        List<SoulUserReport> soulUserReports = mongoTemplate.find(query, SoulUserReport.class);
        return soulUserReports;
    }
}
